{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GPU: True\n",
      "len of train data: 25000\n",
      "len of test data: 25000\n",
      "['Piece', 'of', 'subtle', 'art', '.', 'Maybe', 'a', 'masterpiece', '.', 'Doubtlessly', 'a', 'special', 'story', 'about', 'the', 'ambiguity', 'of', 'existence', '.', 'Tale', 'in', 'Kafka', 'style', 'about', 'impossibility', 'of', 'victory', 'or', 'surviving', 'in', 'a', 'perpetual', 'strange', 'world', '.', 'The', 'life', 'is', ',', 'in', 'this', 'film', ',', 'only', 'exercise', 'of', 'adaptation', '.', 'Lesson', 'about', 'limits', 'and', 'original', 'sin', ',', 'about', 'the', 'frailty', 'of', 'innocence', 'and', 'error', 'of', 'his', 'ways.<br', '/><br', '/>Leopold', 'Kessle', 'is', 'another', 'Joseph', 'K.', 'Images', 'of', 'Trial', 'and', 'same', 'ambiguous', 'woman', '.', 'And', 'Europa', 'is', 'symbol', 'of', 'basic', 'crisis', 'who', 'has', 'many', 'aspects', 'like', 'chimeric', 'wars', 'or', 'unavailing', 'search', 'of', 'truth', '/', 'essence', '/', 'golden', 'age.<br', '/><br', '/>Methaphor', 'or', 'parable', ',', 'the', 'movie', 'is', 'history', 'of', 'disappointed', \"'s\", 'evolution', '.', 'War', ',', 'peace', ',', 'business', 'or', 'lie', 'are', 'only', 'details', 'of', 'gelatin', '-', 'time', '.', 'Hypocrisy', 'is', 'a', 'mask', '.', 'Love-', 'a', 'convention', '.', 'The', 'sacrifice-', 'only', 'method', 'to', 'hope', 'understanding', 'a', 'painful', 'reality', '.']\n",
      "pos\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim\n",
    "from torchtext import data,datasets\n",
    "print('GPU:', torch.cuda.is_available())\n",
    "\n",
    "torch.manual_seed(123)\n",
    "\n",
    "TEXT = data.Field(tokenize='spacy')\n",
    "LABEL = data.LabelField(dtype = torch.float)\n",
    "train_data,test_data = datasets.IMDB.splits(TEXT,LABEL)\n",
    "#都是25000条数据\n",
    "print('len of train data:', len(train_data))\n",
    "print('len of test data:', len(test_data))\n",
    "\n",
    "print(train_data.examples[15].text)\n",
    "print(train_data.examples[15].label)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://nlp.stanford.edu/data/glove.6B.zip\n",
      "-----------------------------\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "TEXT.build_vocab(train_data, max_size=10000, vectors='glove.6B.100d')\n",
    "LABEL.build_vocab(train_data)\n",
    "\n",
    "batchsz = 30\n",
    "device = torch.device('cuda')\n",
    "train_iterator, test_iterator = data.BucketIterator.splits(\n",
    "    (train_data, test_data),\n",
    "    batch_size = batchsz\n",
    "    #device=device\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "class RNN(nn.Module):\n",
    "    def __init__(self,vocab_size,embedding_size,hidden_size):\n",
    "        super(RNN,self).__init__()\n",
    "        self.embedding = nn.Embedding(vocab_size,embedding_size)\n",
    "        self.lstm = nn.LSTM(input_size = embedding_size,hidden_size=hidden_size,num_layers = 2,bidirectional=True,dropout = 0.5)\n",
    "        self.fc = nn.Linear(hidden_size*2,1)\n",
    "        self.dropout = nn.Dropout(0.5)\n",
    "        \n",
    "    def forward(self,x):\n",
    "        #x:seq_len batch_size \n",
    "        embedding = self.dropout(self.embedding(x))#seq_len batch_size embedding_size\n",
    "        #output (seq_len,batch_size,hidden_size) \n",
    "        #hidden (num_layers,batch_size,hidden_size)\n",
    "        #cell同上\n",
    "        output,(hidden,cell) = self.lstm(embedding)\n",
    "        #hidden (batch_size,hidden_size*2)\n",
    "        hidden = torch.cat([hidden[-2],hidden[-1]],dim=1)\n",
    "        hidden = self.dropout(hidden)\n",
    "        out = self.fc(hidden)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = RNN(len(TEXT.vocab),100,256)\n",
    "\n",
    "pretrained_embedding = TEXT.vocab.vectors\n",
    "model.embedding.weight.data.copy_(pretrained_embedding)\n",
    "\n",
    "optimizer = optim.Adam(model.parameters(),lr=3e-4)\n",
    "criterion = nn.BCEWithLogitsLoss().to(device)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch:0\n",
      "[0/834] acc:0.36666667461395264\n"
     ]
    }
   ],
   "source": [
    "def binary_acc(preds, y):\n",
    "    \"\"\"\n",
    "    get accuracy\n",
    "    \"\"\"\n",
    "    preds = torch.round(torch.sigmoid(preds))\n",
    "    correct = torch.eq(preds, y).float()\n",
    "    acc = correct.sum() / len(correct)\n",
    "    return acc\n",
    "\n",
    "for i in range(10):\n",
    "    print('epoch:{}'.format(i))\n",
    "    avg_acc = []\n",
    "    model.train()\n",
    "    for index,data in enumerate(train_iterator):\n",
    "    #     print(index)\n",
    "    #     print(data.text.shape)#torch.Size([927, 30]) torch.Size([949, 30])...\n",
    "        logits = model(data.text).squeeze(1)\n",
    "        loss = criterion(logits,data.label)\n",
    "        acc = binary_acc(logits,data.label).item()\n",
    "        avg_acc.append(acc)\n",
    "        optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "\n",
    "        if index % 100 ==0:\n",
    "            print('[{}/{}] acc:{}'.format(index,len(train_iterator),acc))\n",
    "\n",
    "    avg_acc = np.array(avg_acc).mean()\n",
    "    print('avg acc:', avg_acc) \n",
    "    \n",
    "    avg_acc = []\n",
    "    rnn.eval()\n",
    "    with torch.no_grad():\n",
    "        for batch in test_iterator:\n",
    "            # [b, 1] => [b]\n",
    "            pred = rnn(batch.text).squeeze(1)\n",
    "            loss = criteon(pred, batch.label)\n",
    "            acc = binary_acc(pred, batch.label).item()\n",
    "            avg_acc.append(acc)\n",
    "    avg_acc = np.array(avg_acc).mean()\n",
    "    print('>>test:', avg_acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
